package com.usian.yygh.controller.api;

import com.usian.yygh.common.exception.YyghException;
import com.usian.yygh.common.helper.HttpRequestHelper;
import com.usian.yygh.common.result.Result;
import com.usian.yygh.common.result.ResultCodeEnum;
import com.usian.yygh.common.util.MD5;
import com.usian.yygh.model.hosp.Hospital;
import com.usian.yygh.service.DepartmentService;
import com.usian.yygh.service.HospitalService;
import com.usian.yygh.service.HospitalSetService;
import com.usian.yygh.service.ScheduleService;
import com.usian.yygh.vo.hosp.DepartmentQueryVo;
import com.usian.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author:WGG
 * @Date 2023年01月06日 17:49
 */
@Api(tags = "医院管理的API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;


    @Autowired
    private ScheduleService scheduleService;


    /**
     * 查询医院信息
     * @param request
     * @return
     */
    @ApiOperation(value = "查询医院信息")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将数组转换成对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //查看医院是否存在
        //1.拿到经过MD5加密的签名秘钥
        String sign = map.get("sign").toString();
        //2.根据hoscode查找数据库签名秘钥信息
        String hoscode = map.get("hoscode").toString();
        String keySign = hospitalSetService.getKeySign(hoscode);
        //3.对数据库秘钥MD5加密
        String Md5KeySign = MD5.encrypt(keySign);
        //4.判断签名秘钥是否正确
        if (!sign.equals(Md5KeySign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hosptial = hospitalService.getHosptial(hoscode);
        return Result.ok(hosptial);
    }


    /**
     * 上传医院信息
     * @param request
     * @return
     */
    @ApiOperation(value = "上传医院信息")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将数组转换成对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //查看医院是否存在
        //1.拿到经过MD5加密的签名秘钥
        String sign = map.get("sign").toString();
        //2.根据hoscode查找数据库签名秘钥信息
        String hoscode = map.get("hoscode").toString();
        String keySign = hospitalSetService.getKeySign(hoscode);
        //3.对数据库秘钥MD5加密
        String Md5KeySign = MD5.encrypt(keySign);
        //4.判断签名秘钥是否正确
        if (!sign.equals(Md5KeySign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //5.由于图片被Base64位加密 到数据库的’+‘被换成‘ ’，因此需要转换  图片查看方式<img src="data:image/png;base64,转换后的图片字符串>
        //5.1 拿到图片信息
        String logoData = map.get("logoData").toString();
        //5.2 字符替换
        if (!StringUtils.isEmpty(logoData)){
            String repLogoData = logoData.replaceAll(" ", "+");
            //5.3 存入集合
            map.put("logoData", repLogoData);
        }

        //调用方法添加
        hospitalService.save(map);
        return Result.ok();
    }




    /**
     * 上传科室信息
     * @param request
     * @return
     */
    @ApiOperation(value = "上传科室信息")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将数组转换成对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //查看医院是否存在
        //1.拿到经过MD5加密的签名秘钥
        String sign = map.get("sign").toString();
        //2.根据hoscode查找数据库签名秘钥信息
        String hoscode = map.get("hoscode").toString();
        String keySign = hospitalSetService.getKeySign(hoscode);
        //3.对数据库秘钥MD5加密
        String Md5KeySign = MD5.encrypt(keySign);
        //4.判断签名秘钥是否正确
        if (!sign.equals(Md5KeySign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用方法添加
        departmentService.save(map);
        return Result.ok();
    }


    /**
     * 查询科室信息
     * @param request
     * @return
     */
    @ApiOperation(value = "查询科室信息")
    @PostMapping("/department/list")
    public Result departmentList(HttpServletRequest request){
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将string数组转换成对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //查看医院是否存在
        //1.拿到经过MD5加密的签名秘钥
        String sign = map.get("sign").toString();
        //2.根据hoscode查找数据库签名秘钥信息
        String hoscode = map.get("hoscode").toString();
        String keySign = hospitalSetService.getKeySign(hoscode);
        //3.对数据库秘钥MD5加密
        String Md5KeySign = MD5.encrypt(keySign);
        //4.判断签名秘钥是否正确
        if (!sign.equals(Md5KeySign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //拿到分页信息
        Integer page = StringUtils.isEmpty(parameterMap.get("page")) ? 1 :Integer.parseInt(map.get("page").toString());
        Integer limit = StringUtils.isEmpty(parameterMap.get("limit")) ? 10 :Integer.parseInt(map.get("limit").toString());
        //条件
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //查询
        Page pageAll = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageAll);

    }

    /**
     * 删除科室信息
     * @param request
     * @return
     */
    @ApiOperation(value = "删除科室信息")
    @PostMapping("/department/remove")
    public Result departmentRemove(HttpServletRequest request){
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将string数组转换成对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //查看医院是否存在
        //1.拿到经过MD5加密的签名秘钥
        String sign = map.get("sign").toString();
        //2.根据hoscode查找数据库签名秘钥信息
        String hoscode = map.get("hoscode").toString();
        String keySign = hospitalSetService.getKeySign(hoscode);
        //3.对数据库秘钥MD5加密
        String Md5KeySign = MD5.encrypt(keySign);
        //4.判断签名秘钥是否正确
        if (!sign.equals(Md5KeySign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //执行删除
        departmentService.remove(map);
        return Result.ok();
    }


    /**
     * 上传排班信息
     * @param request
     * @return
     */
    @ApiOperation(value = "上传排班信息")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将数组转换成对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //查看医院是否存在
        //1.拿到经过MD5加密的签名秘钥
        String sign = map.get("sign").toString();
        //2.根据hoscode查找数据库签名秘钥信息
        String hoscode = map.get("hoscode").toString();
        String keySign = hospitalSetService.getKeySign(hoscode);
        //3.对数据库秘钥MD5加密
        String Md5KeySign = MD5.encrypt(keySign);
        //4.判断签名秘钥是否正确
        if (!sign.equals(Md5KeySign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用方法添加
        scheduleService.save(map);
        return Result.ok();
    }


    /**
     * 查询排班信息
     * @param request
     * @return
     */
    @ApiOperation(value = "查询排班信息")
    @PostMapping("/schedule/list")
    public Result scheduleList(HttpServletRequest request){
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将string数组转换成对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //查看医院是否存在
        //1.拿到经过MD5加密的签名秘钥
        String sign = map.get("sign").toString();
        //2.根据hoscode查找数据库签名秘钥信息
        String hoscode = map.get("hoscode").toString();
        String keySign = hospitalSetService.getKeySign(hoscode);
        //3.对数据库秘钥MD5加密
        String Md5KeySign = MD5.encrypt(keySign);
        //4.判断签名秘钥是否正确
        if (!sign.equals(Md5KeySign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //拿到分页信息
        Integer page = StringUtils.isEmpty(parameterMap.get("page")) ? 1 :Integer.parseInt(map.get("page").toString());
        Integer limit = StringUtils.isEmpty(parameterMap.get("limit")) ? 10 :Integer.parseInt(map.get("limit").toString());
        //条件
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
//        scheduleQueryVo.setDepcode(map.get("depcode").toString());
        //查询
        Page pageAll = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageAll);

    }

    /**
     * 删除科室信息
     * @param request
     * @return
     */
    @ApiOperation(value = "删除科室信息")
    @PostMapping("/schedule/remove")
    public Result scheduleRemove(HttpServletRequest request){
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将string数组转换成对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //查看医院是否存在
        //1.拿到经过MD5加密的签名秘钥
        String sign = map.get("sign").toString();
        //2.根据hoscode查找数据库签名秘钥信息
        String hoscode = map.get("hoscode").toString();
        String keySign = hospitalSetService.getKeySign(hoscode);
        //3.对数据库秘钥MD5加密
        String Md5KeySign = MD5.encrypt(keySign);
        //4.判断签名秘钥是否正确
        if (!sign.equals(Md5KeySign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //执行删除
        scheduleService.remove(map);
        return Result.ok();
    }
}
